using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Net.Mail;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;

namespace BPCSFileDaemon.FileFactory
{
    public class EmployeeFileFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected int LineLength
        {
            get
            {
                return (this.EntityID.CompareTo("TW") == 0)?169:172;
            }
        }        

        public EmployeeFileFactory(string path, string DBConnectionString, TypeOfDatabase DatabaseType, string Entity)
            : base(path, DBConnectionString, DatabaseType)
        {
            base.EntityID = Entity;
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();
                    
                    using (RawEmployeeDataManager CurDataManager = new RawEmployeeDataManager(this.CurrentSystemSession))
                    {
                        // execute pre-action before inject data
                        CurDataManager.ExecutePreInjectAction();

                        // start to analyze
                        for (int x = 0; x < this.FileLines.Count; x++)
                        {
                            try
                            {
                                string CurrentLine = this.FileLines[x].ToString();

                                // break by offset
                                if (this.EntityID.CompareTo("CN") == 0 || this.EntityID.CompareTo("RC") == 0)
                                {
                                    #region China

                                    if (CurrentLine.Length >= this.LineLength)  // 168 is the only length
                                    {
                                        string CountryID = CurrentLine.Substring(0, 2).Trim();    // size: 2
                                        string CompanyCode = CurrentLine.Substring(2, 2).Trim();    // size: 2
                                        string EmployeeID = CurrentLine.Substring(4, 6).Trim();    // size: 6
                                        string ActualName = CurrentLine.Substring(10, 50).Trim();      // size: 50
                                        //string LocalName = CurrentLine.Substring(60, 40).Trim();      // size: 60
                                        string EmailID = CurrentLine.Substring(60, 50).Trim();    // size: 50
                                        string CostCenter = CurrentLine.Substring(110, 10).Trim();    // size: 10
                                        //added by agnes
                                        string Email = CurrentLine.Substring(120, 65).Trim();  // size: 60
                                        string VendorID = CurrentLine.Substring(185,5).Trim();   // size: 15
                                        string LocalName = CurrentLine.Substring(190).Trim();

                                        CurDataManager.AddData(this.EntityID, CountryID, CompanyCode, EmployeeID, ActualName, LocalName, EmailID, CostCenter, Email, VendorID);
                                    }

                                    #endregion
                                }
                                else if (this.EntityID.CompareTo("TW") != 0)
                                {
                                    #region Non-Taiwan

                                    if (CurrentLine.Length >= this.LineLength)  // 174 is the only length
                                    {
                                        string CountryID = CurrentLine.Substring(0, 2).Trim();    // size: 2
                                        string CompanyCode = CurrentLine.Substring(2, 2).Trim();    // size: 2
                                        string EmployeeID = CurrentLine.Substring(4, 6).Trim();    // size: 6
                                        string ActualName = CurrentLine.Substring(10, 50).Trim();      // size: 50
                                        string LocalName = CurrentLine.Substring(60, 60).Trim();      // size: 60
                                        string EmailID = CurrentLine.Substring(120, 50).Trim();    // size: 50
                                        string CostCenter = CurrentLine.Substring(170, 10).Trim();    // size: 10
                                        //added by agnes
                                        string Email = CurrentLine.Substring(180, 65).Trim();
                                        string VendorID = CurrentLine.Substring(245).Trim();

                                        CurDataManager.AddData(this.EntityID, CountryID, CompanyCode, EmployeeID, ActualName, LocalName, EmailID, CostCenter, Email, VendorID);
                                    }

                                    #endregion
                                }
                                else
                                {
                                    #region Taiwan - Multilingual

                                    if (CurrentLine.Length >= this.LineLength)  // 169 is the only length
                                    {
                                        string CountryID = CurrentLine.Substring(0, 2).Trim();    // size: 2
                                        string CompanyCode = CurrentLine.Substring(2, 2).Trim();    // size: 2
                                        string EmployeeID = CurrentLine.Substring(4, 6).Trim();    // size: 6
                                        string ActualName = CurrentLine.Substring(10, 50).Trim();      // size: 50
                                        string LocalName = CurrentLine.Substring(60, 55).Trim();      // size: 60
                                        string EmailID = CurrentLine.Substring(115, 50).Trim();    // size: 50
                                        string CostCenter = CurrentLine.Substring(165, 10).Trim();    // size: 10
                                        //added by agnes
                                        string Email = CurrentLine.Substring(175, 65).Trim();  // size: 60
                                        string VendorID = CurrentLine.Substring(240).Trim();   // size: 15

                                        CurDataManager.AddData(this.EntityID, CountryID, CompanyCode, EmployeeID, ActualName, LocalName, EmailID, CostCenter, Email, VendorID);
                                    }

                                    #endregion
                                }
                            }
                            catch (Exception ex)
                            {
                                smooth = false;
                                this.ErrMsg = "[EmployeeFileFactory.ReadFile] : Exception : " + ex.Message;  
                            }
                        }   // for

                        // inject now
                        if (CurDataManager.TotalInTheList > 0)
                        {
                            CurDataManager.ExecuteFinalAction();

                            if (!CurDataManager.ExecuteDataInject())
                            {

                                smooth = false;
                                this.ErrMsg = "[EmployeeFileFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;                               
                                
                            }
                        }
                    }
                }
            } 

           
            return smooth;
        }

     

    }
}
